﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace qf_cms.admin
{
	public partial class add_article : SessionPage
	{
		LogicLayer mgr = new LogicLayer();
		protected string id = "", pid = "", tip = "", colName = "";
		protected CMS_Article curArticle;

		protected void Page_Load(object sender, EventArgs e)
		{
			id = Request.QueryString["id"] + "";
			pid = Request.QueryString["pid"] + "";

			if (!string.IsNullOrEmpty(id))
				curArticle = mgr.GetCMS_Article(id);
			if (curArticle == null)
				curArticle = new CMS_Article();

			if (IsPostBack)
			{
				if (string.IsNullOrEmpty(id))
				{
					//add
					CMS_Article article = new CMS_Article();
					article.ColumnID = pid;
					article.ArticleID = Guid.NewGuid().ToString();
					article.ArticleTitle = Request.Form["articleTitle"];
					article.ArticleContent = CKEditor1.Text;// Request.Form["hidContent"];
					article.SequenceNo = (mgr.GetArticleSum(pid) + 1).ToString().PadLeft(2,'0');
					//article.TemplateID = Request.Form["templates"];
					//if (!string.IsNullOrEmpty(Request.Form["isDefault"]))
					//    article.IsDefault = 1;
					//else
					//    article.IsDefault = 0;

					if (mgr.AddCMS_Article(article))
						Response.Redirect("columnGeneral.aspx?id=" + pid);
					else
						tip = "保存失败!";
				}
				else
				{
					//edit
					CMS_Article article = mgr.GetCMS_Article(id);
					article.ArticleTitle = Request.Form["articleTitle"];
					article.ArticleContent = CKEditor1.Text;// Request.Form["hidContent"];
					//article.TemplateID = Request.Form["templates"];

					if (mgr.UpdateCMS_Article(article))
						Response.Redirect("columnGeneral.aspx?id=" + pid);
					else
						tip = "保存失败!";
				}
			}

			//ckeditor config
			CKEditor1.BasePath = "/scripts/ckeditor";
			CKEditor1.BaseHref = "/scripts/ckeditor";

			CKEditor1.Text = curArticle.ArticleContent;

			CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
			//string datadir = System.Configuration.ConfigurationManager.AppSettings["datadir"];
			//datadir = System.IO.Path.Combine(datadir, "att");
			_FileBrowser.BasePath = "/scripts/ckfinder";
			_FileBrowser.SetupCKEditor(CKEditor1);
		}

		protected void WriteTemplateOption()
		{
			string[] tempNameArray = new string[] { "subArticle", "subColumn", "viewArticle" };
			string[] tempArray = new string[] { "subArticle", "subColumn", "viewArticle" };
			for (int i = 0; i < tempNameArray.Length;i++ )
			{
				if (curArticle.TemplateID == tempArray[i])
					Response.Write("<option value=\"" + tempArray[i] + "\" selected >" + tempNameArray[i] + "</option>");
				else
					Response.Write("<option value=\"" + tempArray[i] + "\">" + tempNameArray[i] + "</option>");
			}
		}
	}
}